/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.hiof.moraa.server.dao;

import com.hiof.moraa.server.service.model.Feed;
import com.hiof.moraa.server.service.model.PassTargetFeed;
import com.hiof.moraa.mapping.TblAthlete;
import com.hiof.moraa.mapping.TblPassTarget;
import com.hiof.moraa.mapping.TblSupport;
import com.hiof.moraa.mapping.TblTarget;
import java.util.Collection;
import java.util.List;
import java.util.logging.Logger;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.logging.Level;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

/**
 *
 * @author Bergy
 */
public class HibernatePassTargetDAOImp implements PassTargetDAO {

    private SessionFactory factory;

    public SessionFactory getFactory() {
        return factory;
    }

    public void setFactory(SessionFactory factory) {
        this.factory = factory;
    }

    @Override
    public PassTargetFeed insertPassTargetData(int targetId, int athleteId, int supportId, Timestamp timestamp) {
        TblPassTarget passTarget = new TblPassTarget();

        try {
            TblTarget target = (TblTarget) factory.getCurrentSession().get(TblTarget.class, targetId);
            TblAthlete athlete = (TblAthlete) factory.getCurrentSession().get(TblAthlete.class, athleteId);
            TblSupport support = (TblSupport) factory.getCurrentSession().get(TblSupport.class, supportId);

            passTarget.setTblTarget(target);
            passTarget.setTblAthlete(athlete);
            passTarget.setTblSupport(support);
            passTarget.setTimeStamp(timestamp);
            factory.getCurrentSession().save(passTarget);

        } catch (HibernateException e) {
            Logger.getLogger(HibernatePassTargetDAOImp.class.getName()).log(Level.SEVERE, "Hibernate Exception Occured", e);
        }

        PassTargetFeed passTargetFeed = convert2PassTargetFeed(passTarget);
        return passTargetFeed;
    }

    @Override
    public List<PassTargetFeed> getLatestPassTargetFeedOfAllAthletesInOneRoute(int routeId, int numOfFeed) {
        Session session = factory.getCurrentSession();

        String hql = "from TblPassTarget media where media.tblAthlete.tblRoute.id = :routeId "
                + "order by media.timeStamp desc";
        Query query = session.createQuery(hql);
        query.setInteger("routeId", routeId);
        query.setMaxResults(numOfFeed);
        List<TblPassTarget> passTargets = query.list();

        List<PassTargetFeed> result = fetchToPassTargetFeed(passTargets);
        passTargets.clear();
        return result;
    }

    @Override
    public Collection<? extends Feed> getLatestPassTargetFeedOfOneAthleteInOneRoute(String username, int routeId, int numOfPassTargetFeed) {
        Session session = factory.getCurrentSession();

        String hql = "from TblPassTarget media where media.tblAthlete.tblUser.username = :username and media.tblAthlete.tblRoute.id = :routeId "
                + "order by media.timeStamp desc";
        Query query = session.createQuery(hql);
        query.setString("username", username);
        query.setInteger("routeId", routeId);
        query.setMaxResults(numOfPassTargetFeed);
        List<TblPassTarget> passTargets = query.list();

        List<PassTargetFeed> result = fetchToPassTargetFeed(passTargets);
        passTargets.clear();
        return result;
    }

    private List<PassTargetFeed> fetchToPassTargetFeed(List<TblPassTarget> passTargets) {
        List<PassTargetFeed> result = new ArrayList<PassTargetFeed>();
        for (TblPassTarget passTarget : passTargets) {
            result.add(convert2PassTargetFeed(passTarget));
        }
        return result;
    }

    private PassTargetFeed convert2PassTargetFeed(TblPassTarget passTarget) {
        PassTargetFeed passTargetFeed = new PassTargetFeed(
                passTarget.getTblAthlete().getTblUser().getId(),
                passTarget.getTblAthlete().getTblUser().getUsername(),
                passTarget.getTblAthlete().getTblUser().getFirstName() + " " +   passTarget.getTblAthlete().getTblUser().getLastName(),
                passTarget.getTimeStamp(),
                passTarget.getTblAthlete().getAttendantCode(),
                passTarget.getTblAthlete().getTblUser().getProfilePicture(),
                passTarget.getTblTarget().getId(),
                passTarget.getTblTarget().getType());
        return passTargetFeed;
    }
}
